//
//  ProcurementHallViewController.m
//  PlayerWorld
//
//  Created by 1 on 16/4/28.
//  Copyright © 2016年 刘成. All rights reserved.
//
#define UISCREEN_WIDTH  [UIScreen mainScreen].bounds.size.width
#define UISCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#import "ProcurementHallViewController.h"
#import "ProcurementHallTableViewCell.h"
#import "LotteryInformat1TableViewCell.h"
#import "PLFiveViewController.h"
#import "UIKit+AFNetworking.h"
#import "ProcurementHallModel.h"
#import "ChoosePlayViewController.h"
@interface ProcurementHallViewController ()

@end

@implementation ProcurementHallViewController
{
    NSMutableArray *_ProcurementHallArray;
}

-(void)obtainData
{
    // 启动系统风火轮
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    //服务器给的域名
    NSString *domainStr = @"http://666.lefun6.com/";

    //创建一个可变字典
    NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
    //往字典里面添加需要提交的参数
    [parametersDic setObject:@"System" forKey:@"Model"];
    [parametersDic setObject:@"GetSystemConfig" forKey:@"Action"];
    [parametersDic setObject:@"HX_Game_Name" forKey:@"models"];

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    //以post的形式提交，POST的参数就是上面的域名，parameters的参数是一个字典类型，将上面的字典作为它的参数
    [manager POST:domainStr parameters:parametersDic progress:^(NSProgress * _Nonnull uploadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 解析成功隐藏系统风火轮
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];

        _ProcurementHallArray = [[NSMutableArray alloc]init];

        for (NSDictionary *modeldic in [dic objectForKey:@"HX_Game_Name"]) {

            ProcurementHallModel *model = [[ProcurementHallModel alloc]init];
            [model setValuesForKeysWithDictionary:modeldic];
            if ([model.G_N_Off isEqualToString:@"1"]) {
                [_ProcurementHallArray addObject:model];
            }
        }
        [ProcurementHalltableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        // 解析失败隐藏系统风火轮(可以打印error.userInfo查看错误信息)
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
    }];
    
}


- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UILabel *atitlelabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 30)];
    atitlelabel.text = @"购 彩 大 厅";
    atitlelabel.textAlignment = NSTextAlignmentCenter;
    atitlelabel.font = [UIFont boldSystemFontOfSize:17];
    atitlelabel.textColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.titleView = atitlelabel;
    self.view.backgroundColor = [UIColor orangeColor];
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:nil];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:36/255.0f green:11/255.0f blue:54/255.0f alpha:1];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};

    ProcurementHalltableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, UISCREEN_HEIGHT-64) style:UITableViewStylePlain];
    ProcurementHalltableView.dataSource = self;
    ProcurementHalltableView.delegate = self;
    [ProcurementHalltableView registerNib:[UINib nibWithNibName:@"LotteryInformat1TableViewCell" bundle:nil]  forCellReuseIdentifier:@"cell"];
    ProcurementHalltableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:ProcurementHalltableView];

    [self obtainData];
}

- (CGFloat)tableView:(UITableView *)atableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 49;
}

//设置表视图中的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _ProcurementHallArray.count;
    
}

//设置单元格中的数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdent = @"cell";
    LotteryInformat1TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdent];
//    if (cell == nil) {
//        cell = [[LotteryInformat1TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdent];
//    }
//    cell.backgroundColor = [UIColor whiteColor];
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//
//    ProcurementHallModel *model = [_ProcurementHallArray objectAtIndex:indexPath.row];
//    cell.titleLabel.text = model.G_N_Title;
    
    cell.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    ProcurementHallModel *model = [_ProcurementHallArray objectAtIndex:indexPath.row];
    cell.lotteryTitleLabel.text = model.G_N_Title;
    return cell;
}
//单元格点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ChoosePlayViewController *view = [[ChoosePlayViewController alloc] init];
    view.G_N_Id = [[_ProcurementHallArray objectAtIndex:indexPath.row] G_N_Id];
    view.name = [[_ProcurementHallArray objectAtIndex:indexPath.row] G_N_Title];
    [view setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:view animated:YES];
//    PLFiveViewController *cHDVC= [[PLFiveViewController alloc] init];
//    cHDVC.G_N_Id = [[_ProcurementHallArray objectAtIndex:indexPath.row] G_N_Id];
//    [cHDVC setHidesBottomBarWhenPushed:YES];
//    [self.navigationController pushViewController:cHDVC animated:YES];

   }

@end
